﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.OleDb;

namespace 自消费
{
    [Serializable]
    public class CardData
    {

        public const string BALANCEField = "@balance";

        public const string CUSTOMER_PWDField = "@customer_pwd";

        public const string EX_SETTINGField = "@ex_setting";

        public const string EXPIRE_DATEField = "@expire_date";

        public const string ICCIDField = "@iccid";

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string IMEIField = "@imei";

        public const string MSISDNField = "@msisdn";

        public const string TOTALCOUNT_SENDSMSField = "@totalcount_sendsms";

        private Single _balance;

        private String _customer_pwd;

        private String _ex_setting;

        private DateTime _expire_date;

        private String _iccid;

        private String _ID;

        private String _imei;

        private String _msisdn;

        private Int32 _totalcount_sendsms;

        public Single balance
        {
            get { return _balance; }
            set { _balance = value; }
        }
        public String customer_pwd
        {
            get { return _customer_pwd; }
            set { _customer_pwd = value; }
        }
        public String ex_setting
        {
            get { return _ex_setting; }
            set { _ex_setting = value; }
        }
        public DateTime expire_date
        {
            get { return _expire_date; }
            set { _expire_date = value; }
        }
        public String iccid
        {
            get { return _iccid; }
            set { _iccid = value; }
        }
        public String ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String imei
        {
            get { return _imei; }
            set { _imei = value; }
        }
        public String msisdn
        {
            get { return _msisdn; }
            set { _msisdn = value; }
        }
        public Int32 totalcount_sendsms
        {
            get { return _totalcount_sendsms; }
            set { _totalcount_sendsms = value; }
        }
        public CardData()
        {
            _balance = 0;
            _customer_pwd = "";
            _ex_setting = "";
            _expire_date = DateTime.Now;
            _iccid = "";
            _ID = "";
            _imei = "";
            _msisdn = "";
            _totalcount_sendsms = 0;
        }
    }

    public class CardDataFactory
    {
        public static CardData Construct(IDataReader reader)
        {
            CardData data = new CardData();

            try { data.balance = Convert.ToSingle(reader["balance"]); }
            catch { }

            try { data.customer_pwd = Convert.ToString(reader["customer_pwd"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(reader["ex_setting"]); }
            catch { }

            try { data.expire_date = Convert.ToDateTime(reader["expire_date"]); }
            catch { }

            try { data.iccid = Convert.ToString(reader["iccid"]); }
            catch { }

            try { data.ID = Convert.ToString(reader["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(reader["imei"]); }
            catch { }

            try { data.msisdn = Convert.ToString(reader["msisdn"]); }
            catch { }

            try { data.totalcount_sendsms = Convert.ToInt32(reader["totalcount_sendsms"]); }
            catch { }

            return data;
        }
        public static String GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static String GetPrimaryKey(DataTable dt, int rowIndex)
        {
            String PrimaryKey = "";

            if (dt.Rows.Count < rowIndex + 1)

                return "";

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return "";

            try { PrimaryKey = Convert.ToString(dr[CardData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static CardData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static CardData Construct(DataTable dt, int rowIndex)
        {
            CardData data = new CardData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.balance = Convert.ToSingle(dr["balance"]); }
            catch { }

            try { data.customer_pwd = Convert.ToString(dr["customer_pwd"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(dr["ex_setting"]); }
            catch { }

            try { data.expire_date = Convert.ToDateTime(dr["expire_date"]); }
            catch { }

            try { data.iccid = Convert.ToString(dr["iccid"]); }
            catch { }

            try { data.ID = Convert.ToString(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.msisdn = Convert.ToString(dr["msisdn"]); }
            catch { }

            try { data.totalcount_sendsms = Convert.ToInt32(dr["totalcount_sendsms"]); }
            catch { }

            return data;
        }
        public static CardData Construct(DataGridView dgv, int rowIndex)
        {
            CardData data = new CardData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.balance = Convert.ToSingle(dr["balance"]); }
            catch { }

            try { data.customer_pwd = Convert.ToString(dr["customer_pwd"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(dr["ex_setting"]); }
            catch { }

            try { data.expire_date = Convert.ToDateTime(dr["expire_date"]); }
            catch { }

            try { data.iccid = Convert.ToString(dr["iccid"]); }
            catch { }

            try { data.ID = Convert.ToString(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.msisdn = Convert.ToString(dr["msisdn"]); }
            catch { }

            try { data.totalcount_sendsms = Convert.ToInt32(dr["totalcount_sendsms"]); }
            catch { }

            return data;
        }
        public static String GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            String PrimaryKey = "";

            if (dgv.Rows.Count < rowIndex + 1)

                return "";

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return "";

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == CardData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToString(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
        public static CardData Construct(DataRow dr)
        {
            CardData data = new CardData();

            try { data.balance = Convert.ToSingle(dr["balance"]); }
            catch { }

            try { data.customer_pwd = Convert.ToString(dr["customer_pwd"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(dr["ex_setting"]); }
            catch { }

            try { data.expire_date = Convert.ToDateTime(dr["expire_date"]); }
            catch { }

            try { data.iccid = Convert.ToString(dr["iccid"]); }
            catch { }

            try { data.ID = Convert.ToString(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.msisdn = Convert.ToString(dr["msisdn"]); }
            catch { }

            try { data.totalcount_sendsms = Convert.ToInt32(dr["totalcount_sendsms"]); }
            catch { }

            return data;
        }
        public static String GetPrimaryKey(DataRow dr)
        {
            String PrimaryKey = "";

            try { PrimaryKey = Convert.ToString(dr[CardData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class CardDataDBOption
    {
        public static int Insert(CardData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into Card (");
            sql.Append("balance, ");
            sql.Append("ID, ");
            sql.Append("customer_pwd, ");
            sql.Append("ex_setting, ");
            sql.Append("expire_date, ");
            sql.Append("iccid, ");
            sql.Append("imei, ");
            sql.Append("msisdn, ");
            sql.Append("totalcount_sendsms");
            sql.Append(") values (");
            sql.Append("");
            sql.Append(d.balance.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.customer_pwd.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.ex_setting.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("#");
            sql.Append(d.expire_date.ToString());
            sql.Append("#");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.iccid.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.imei.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.msisdn.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.totalcount_sendsms.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(CardData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update Card set ");
            sql.Append("balance=");
            sql.Append("");
            sql.Append(d.balance.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("customer_pwd=");
            sql.Append("'");
            sql.Append(d.customer_pwd.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("ex_setting=");
            sql.Append("'");
            sql.Append(d.ex_setting.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("expire_date=");
            sql.Append("#");
            sql.Append(d.expire_date.ToString());
            sql.Append("#");
            sql.Append(", ");
            sql.Append("iccid=");
            sql.Append("'");
            sql.Append(d.iccid.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("imei=");
            sql.Append("'");
            sql.Append(d.imei.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("msisdn=");
            sql.Append("'");
            sql.Append(d.msisdn.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("totalcount_sendsms=");
            sql.Append("");
            sql.Append(d.totalcount_sendsms.ToString());
            sql.Append("");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(CardData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from Card ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect(CardData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Card ");
            return WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect(CardData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Card ");
            return WFNetLib.ADO.AccessOP.DataSetSQL(sql.ToString());
        }
        public static OleDbDataReader ReaderSelect(CardData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Card ");
            return WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql.ToString());
        }
    }
}